<?php

class Webkul_Marketplacepartner_Block_Commisions_Grid extends Mage_Adminhtml_Block_Widget_Grid
{
    public function __construct(){
        parent::__construct();
        $this->setId('customerGrid');
        $this->setUseAjax(true);
        $this->setDefaultSort('entity_id');
        $this->setSaveParametersInSession(true);
    }

    protected function _prepareCollection(){
		$mysqlprefix = Mage::getConfig()->getTablePrefix();//code for mysql prefix
		$mytablepartnerstatus=$mysqlprefix.'marketplacepartner_entity_userdata';
		$mytablepayment=$mysqlprefix.'marketplacepartner_entity_datapayment';

		$mytablesaleperuser=$mysqlprefix.'marketplacepartner_entity_saleperpartner';
		$collection1 = Mage::getModel('marketplacepartner/sellerpayment')->getCollection();
		$collection1->getSelect();
		$stack=array();
		foreach ($collection1 as $verifyrow) {
		array_push($stack,$verifyrow->getmageuserid());
		}
		//print_r($stack); die();
        $collection = Mage::getResourceModel('customer/customer_collection')
            ->addNameToSelect()
            ->addAttributeToSelect('email')
            ->addAttributeToSelect('created_at')
            ->addAttributeToSelect('group_id')
            ->joinAttribute('billing_postcode', 'customer_address/postcode', 'default_billing', null, 'left')
            ->joinAttribute('billing_city', 'customer_address/city', 'default_billing', null, 'left')
            ->joinAttribute('billing_telephone', 'customer_address/telephone', 'default_billing', null, 'left')
            ->joinAttribute('billing_region', 'customer_address/region', 'default_billing', null, 'left')
            ->joinAttribute('billing_country_id', 'customer_address/country_id', 'default_billing', null, 'left');
			
		$collection->addAttributeToFilter('entity_id', array('in' => $stack));

		$collection->getSelect()->join($mytablepartnerstatus,'e.entity_id = '.$mytablepartnerstatus.'.mageuserid AND '.$mytablepartnerstatus.'.wantpartner=1'  ,array('wantpartner'=>'wantpartner'));
		$collection->getSelect()->joinLeft($mytablepayment,'e.entity_id = '.$mytablepayment.'.userid',array('paymentsource'=>'paymentsource'));
		$collection->getSelect()->joinLeft($mytablesaleperuser,'e.entity_id = '.$mytablesaleperuser.'.mageuserid',array('totalsale'=>'totalsale','amountrecived'=>'amountrecived','amountpaid'=>'amountpaid','amountremain'=>'amountremain','commision'=>'commision'));
        $this->setCollection($collection);
        parent::_prepareCollection();
        foreach ($this->getCollection() as $item){
			$item->porders = sprintf('<a href="%s" title="Click to see">Partner Orders</a>',
										 $this->getUrl('adminhtml/sales_order/index/partner_id/'. $item->getEntityid())
									    );
			$item->popspecific = sprintf('<a href="%s" title="Click to see">SP</a>', "http://www.google.com");	
		 }
		 return parent::_prepareCollection();
    }

    protected function _prepareColumns(){
        $this->addColumn('entity_id', array(
            'header'    => Mage::helper('customer')->__('ID'),
            'width'     => '50px',
            'index'     => 'entity_id',
            'type'  => 'number',
			'filter'    => false,
            'sortable'  => false
        ));
        $this->addColumn('name', array(
            'header'    => Mage::helper('customer')->__('Name'),
            'index'     => 'name',
			'filter'    => false,
            'sortable'  => false
        ));
        $this->addColumn('email', array(
            'header'    => Mage::helper('customer')->__('Email'),
            'width'     => '150',
            'index'     => 'email',
			'filter'    => false,
            'sortable'  => false
        ));
		// for partner status
		$this->addColumn('wantpartner', array(
            'header'    => Mage::helper('customer')->__('wantpartner'),
            'index'     => 'wantpartner',
			'filter'    => false,
            'sortable'  => false
        ));
		$this->addColumn('commision', array(
            'header'    => Mage::helper('customer')->__('Commision %'),
            'index'     => 'commision',
			'filter'    => false,
            'sortable'  => false
        ));
		$this->addColumn('paymentsource', array(
            'header'    => Mage::helper('customer')->__('Mode Of Payment'),
            'index'     => 'paymentsource',
			'filter'    => false,
            'sortable'  => false
        ));
		$this->addColumn('totalsale', array(
            'header'    => Mage::helper('customer')->__('Total sales'),
            'index'     => 'totalsale',
			'filter'    => false,
            'sortable'  => false
        ));
		$this->addColumn('amountpaid', array(
            'header'    => Mage::helper('customer')->__('Amount Recived'),
            'index'     => 'amountpaid',
			'filter'    => false,
            'sortable'  => false
        ));
		$this->addColumn('amountremain', array(
            'header'    => Mage::helper('customer')->__('Amount Remain'),
            'index'     => 'amountremain',
			'filter'    => false,
            'sortable'  => false
        ));
		$this->addColumn('amountrecived', array( 
            'header'    => Mage::helper('customer')->__('Last Pay Amount'),
            'index'     => 'amountrecived',
			'filter'    => false,
            'sortable'  => false
        ));
		$this->addColumn('Pay',
            array(
                'header'    =>  Mage::helper('customer')->__('Action'),
                'width'     => '100',
                'type'      => 'action',
                'getter'    => 'getId',
                'actions'   => array(array(
										'caption'   => Mage::helper('customer')->__('Pay'),
										'url'       => array('base'   => '*/*/payamount'),
										'field' => 'ID',
										 )
								),
                'filter'    => false,
                'sortable'  => false,
                'index'     => 'stores',
                'is_system' => true,
        ));
		//for partner status
        $this->addColumn('customer_since', array(
            'header'    => Mage::helper('customer')->__('Customer Since'),
            'type'      => 'datetime',
            'align'     => 'center',
            'index'     => 'created_at',
            'gmtoffset' => true,
			'filter'    => false,
            'sortable'  => false
        ));
		
        if (!Mage::app()->isSingleStoreMode()) {
            $this->addColumn('website_id', array(
                'header'    => Mage::helper('customer')->__('Website'),
                'align'     => 'center',
                'width'     => '80px',
                'type'      => 'options',
                'options'   => Mage::getSingleton('adminhtml/system_store')->getWebsiteOptionHash(true),
                'index'     => 'website_id',
            ));
        }
        return parent::_prepareColumns();
    }

    protected function _prepareMassaction(){
        $this->setMassactionIdField('entity_id');
        $this->getMassactionBlock()->setFormFieldName('customer');
		$this->getMassactionBlock()->addItem('payamount', array(
             'label'    => Mage::helper('customer')->__('Pay Amount'),
             'url'      => $this->getUrl('*/*/masspayamount')
        ));
        return $this;
    }
}
